using System;
using System.Collections.Generic;
using System.Text;
using XinBang.Entity;
using System.Web;

namespace XinBang.ShoppingCart
{
    public class Cart
    {
        private Dictionary<int, int> cart;
        public Cart()
        {
            cart = new Dictionary<int, int>();
        }
        
        public void AddGoodsIntoCart(int goods,int amount){
            if (cart.ContainsKey(goods))
            {
                int num = 0;
                cart.TryGetValue(goods,out num);
                cart[goods]= num + amount;
            }
            else
            {
                cart.Add(goods, amount);
            }
        }
        public void RemoveGoodsFromCart(int goods, int amount)
        {
            if (cart.ContainsKey(goods))
            {
                int num = 0;
                cart.TryGetValue(goods, out num);
                int number = (num - amount) > 0 ? (num - amount) : 0;
                if (number == 0)
                {
                    cart.Remove(goods);
                }
                else
                {
                    cart[goods]=number;
                }
            }
        }


        public void ClearShoppingCart()
        {
            cart.Clear();
        }

        public Dictionary<int, int> GetDictionary()
        {
            return cart;
        }

        public static Cart GetShoppingCart()
        {
            return HttpContext.Current.Session["shoppingcart"] as Cart;
        }

        public void RemoveGoodsFromCart(int goodsid)
        {
            if (cart.ContainsKey(goodsid))
            {
                cart.Remove(goodsid);
            }
        }
    }
}
